var maxSlidingWindow = function(nums, k) {

  const len = nums.length;
  const monoQueue = new MonoQueue();
  let ans = [];

  for (let i = 0; i < k; i++) {
    monoQueue.enqueue(nums[i]);
  }
  ans.push(monoQueue.getMax());
  for (let i = k; i < len; i++) {
    monoQueue.enqueue(nums[i]);
    monoQueue.dequeue(nums[i - k]);
    console.log(monoQueue)
    ans.push(monoQueue.getMax());
  }
  return ans;

};

// 维护一个单调递增队列
class MonoQueue {
  constructor() {
    this.queue = [];
  }

  // 进队列
  enqueue(val) {
    let tail = this.queue.length - 1;
    while (tail >= 0 && val > this.queue[tail]) {
      this.queue.pop();
      tail--;
    }
    this.queue.push(val);
  }
  // 出队列
  dequeue(val) {
    if (this.queue[0] === val) {
      this.queue.shift();
    }
  }
  // 返回最大值
  getMax() {
    return this.queue[0];
  }
}

const nums = [1,3,1,2,0,5], k = 3;
console.log(maxSlidingWindow(nums, k))